Read in SF trees data

sf_trees <- read_csv(here("data", "sf_trees","sf_trees.csv"))
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   tree_id = col_double(),
##   legal_status = col_character(),
##   species = col_character(),
##   address = col_character(),
##   site_order = col_double(),
##   site_info = col_character(),
##   caretaker = col_character(),
##   date = col_date(format = ""),
##   dbh = col_double(),
##   plot_size = col_character(),
##   latitude = col_double(),
##   longitude = col_double()
## )

Basic wrangling reminders

Refresh some skills for daata wrangling & summary statistics using functions in the ‘dplyr’ paackage.

Find the top 5 highest observations of trees by legal status, do some wrangling, and make a graph.

# Wrangling on types of tree by legal status
top_5_status <- sf_trees %>% 
  count(legal_status) %>% 
  drop_na(legal_status) %>% #drop NAs in any row where you don't want them based on column variables
  rename(tree_count = n) %>% #new name on the left, old name on the right
  relocate(tree_count) %>% #moves tree_count to the first column position
  #just keep legal_status categories with top tree_count values
  slice_max(tree_count, n = 5) #to identify rows with highest value specified

Make a graph of those top 5 obsesrvations by legal status.

ggplot(data = top_5_status, aes(x = fct_reorder(legal_status, tree_count), y = tree_count)) + #fct_reorder can be reversed (asc vs. desc)
  geom_col() +
  labs(x = "Legal Status",
       y = "Tree Count") +
  coord_flip() + #flips the x and y axes
  theme_minimal()

A few more data wraangling refresher examples

Only want to keep observations (rows) for Blackwood Acacia.

blackwood_acacia_Psite <- sf_trees %>% 
  filter(legal_status == "Permitted Site") #fxn we'll use the most in wrangling data

# Exaample: keep any observations where a certain string is detected anywhere within that variable for an observation
blackwood_acacia <- sf_trees %>% 
  filter(str_detect(species, "Blackwood Acacia")) %>% 
  select(legal_status, date, latitude, longitude) #useful for picking/excluding columns, helpful when there are too many

#Plot trees! (A preview! Because R doesn't know these are geographic points yet)
ggplot(data = blackwood_acacia, aes(x = longitude, y = latitude)) +
  geom_point()
## Warning: Removed 27 rows containing missing values (geom_point).

tidyr::separate() and unite() functions

Useful for combining or separating fxns.

#split columns by separators
sf_trees_sep <- sf_trees %>% 
  separate(species, into = c("spp_scientific","spp_common"), sep = "::")
  # separate(column, into = c("new column name 1", "new column name 2"), sep = "type of separator")

Example: tidyr::unite()

Combine tree and legal_status columns

sf_trees_unite <- sf_trees %>% 
  unite("id_status", tree_id:legal_status, sep = "_cool!_")
  # unite("name of new united column", column 1 to unite:column 2 to unite, sep = "type of separator")

Make some aactual maps of blackwood aacaciaa trees in SF

‘st_as_sf()’ to convert latitude & longitude to spatial coordinates.

# CRS coordinates transforms spherical data for 2d map visualization

blackwood_acacia_sp <- blackwood_acacia %>% 
  drop_na(longitude, latitude) %>% #get rid of observations where lat or long are missing
  st_as_sf(coords = c("longitude", "latitude")) #indicates that variables are storing long and lat data are geographic points. Here in coords = c() you are giving the variable names for the lat and long points

# Assign a data coordinate system of CRS (4326)
st_crs(blackwood_acacia_sp) = 4326

ggplot(data = blackwood_acacia_sp) + 
  geom_sf(color = "darkgreen")+
  theme_void()

Read in SF roads shapefile to give the map meaning!

sf_map <- read_sf(here("data","sf_map", "tl_2017_06075_roads.shp"))

#check using st_transform(sf_map) to make sure it's the correct shp file

#Roads and tree points bust be in the same coordinate system
st_transform(sf_map, 4326)
## Simple feature collection with 4087 features and 4 fields
## geometry type:  LINESTRING
## dimension:      XY
## bbox:           xmin: -122.5136 ymin: 37.70813 xmax: -122.3496 ymax: 37.83213
## geographic CRS: WGS 84
## # A tibble: 4,087 x 5
##    LINEARID   FULLNAME     RTTYP MTFCC                                  geometry
##  * <chr>      <chr>        <chr> <chr>                          <LINESTRING [°]>
##  1 110498938… Hwy 101 S O… M     S1400 (-122.4041 37.74842, -122.404 37.7483, -…
##  2 110498937… Hwy 101 N o… M     S1400 (-122.4744 37.80691, -122.4746 37.80684,…
##  3 110366022… Ludlow Aly … M     S1780 (-122.4596 37.73853, -122.4596 37.73845,…
##  4 110608181… Mission Bay… M     S1400 (-122.3946 37.77082, -122.3929 37.77092,…
##  5 110366689… 25th Ave N   M     S1400 (-122.4858 37.78953, -122.4855 37.78935,…
##  6 110368970… Willard N    M     S1400 (-122.457 37.77817, -122.457 37.77812, -…
##  7 110368970… 25th Ave N   M     S1400 (-122.4858 37.78953, -122.4858 37.78952,…
##  8 110498933… Avenue N     M     S1400 (-122.3643 37.81947, -122.3638 37.82064,…
##  9 110368970… 25th Ave N   M     S1400  (-122.4854 37.78983, -122.4858 37.78953)
## 10 110367749… Mission Bay… M     S1400 (-122.3865 37.77086, -122.3878 37.77076,…
## # … with 4,077 more rows
#Now plot
ggplot(data = sf_map) +
  geom_sf() +
  theme_void()

Combine blackwood acacia tree observations & SF roads map:

ggplot()+
  geom_sf(data = sf_map, size = 0.1, color = "darkgray")+
  geom_sf(data = blackwood_acacia_sp, size = 0.5, color = "red") +
  theme_void()

Wouldn’t it be cool if this map was interactive?!

tmap_mode("view") #set tmap mode to interactive viewing
## tmap mode set to interactive viewing
tm_shape(blackwood_acacia_sp) +
  tm_dots()
#Now you can zoom in! Turns out there's a blackwood acaia tree on my block!